package ma.ensao.egicom.core.dao.implementations;

import java.util.List;

import ma.ensao.egicom.core.dao.interfaces.CategoryDAO;
import ma.ensao.egicom.core.model.Category; 

import org.hibernate.Query;
import org.scub.foundation.framework.core.dao.implementations.HibernateDaoBaseImplementation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CategoryDAOImpl extends HibernateDaoBaseImplementation implements CategoryDAO {

    private static final Logger log = LoggerFactory.getLogger(CategoryDAO.class);

    // property constants
    public static final String ID = "id";

    public static final String NAME = "name";

    public static final String DESCRIPTION = "description";

    /**
     * {@inheritDoc}
     */
    @Override
    public void save(Category transientInstance) {
        save(transientInstance);

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void delete(Category persistentInstance) {
        delete(persistentInstance);

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Category findById(java.lang.Long id) {
        return load(id, Category.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findByProperty(String propertyName, Object value) {
        log.debug("finding Category instance with property: " + propertyName + ", value: " + value);
        try {
            String queryString = "from Category as model where model." + propertyName + "= ?";
            Query queryObject = createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findByName(Object name) {
        return findByProperty(NAME, name);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findByDescription(Object description) {
        return findByProperty(DESCRIPTION, description);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findAll() {
        return loadAll(Category.class);
    }

}
